<template>
  <el-dialog title="添加营业时间" :visible.sync="dialogVisible" @close="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form size="small" :model="formData" :rules="formRules" ref="form">
      <!--开门营业时间-->
      <el-form-item label="开门营业时间" prop="startTime" :label-width="formLabelWidth">
        <el-time-select
          placeholder="开门时间"
          v-model="formData.startTime"
          :picker-options="{
            start: '00:00',
            step: '00:15',
            end: '23:59'
          }">
        </el-time-select>
        <p>提示：营业开始时间</p>
      </el-form-item>
      <!--关门休息时间-->
      <el-form-item label="关门休息时间" prop="endTime" :label-width="formLabelWidth">
        <el-time-select
          placeholder="关门时间"
          v-model="formData.endTime"
          :picker-options="{
            start: '00:00',
            step: '00:15',
            end: '23:59'
          }">
        </el-time-select>
        <p>提示：选择早于设定的开门时间则代表次日时间</p>
      </el-form-item>
    </el-form>
    <!--操作按钮-->
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible">取 消</el-button>
      <el-button type="primary" @click="onSubmit()" :disabled="loading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Shoptime from '@/api/shoptime.js';
import { deepClone,formatModel } from '@/utils/base.js';
export default {
  data() {
    return {
      /*是否加载中*/
      loading: false,
      /*form表单数据对象*/
      formData: {
        /*菜单名称*/
        areaid: '',
        /*排序*/
        sort: 1,
        /*父集ID*/
        parent_id: 0,
        //
        parentsVal:[],
        /*营业时间*/
        startTime:'',
        endTime:'',
      },
      /*验证规则*/
      formRules: {
        parentsVal: [{ required: true, message: '请选择区域', trigger: 'change' }],
        startTime: [{ required: true, message: '请选择开门营业时间', trigger: 'change' }],
        endTime: [{ required: true, message: '请选择关门休息时间', trigger: 'change' }],
      },
      /*当前父集ID*/
      parentsVal: [],
      /*菜单列表*/
      accessList: [],
      /*排序*/
      srot: '1',
      /*左边长度*/
      formLabelWidth: '120px',
      /*是否显示*/
      dialogVisible: false,
      /*展示数据*/
      propsParam: {
        label: 'label',
        value: 'value',
        checkStrictly: true
      },
      /*时间*/
      timeVal: new Date(2016, 9, 10, 8, 0, 0),
      /*如果没有后面的-8.64e7就是不可以选择今天的*/
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        }
      },
    };
  },
  props: {
    open_add: Boolean,
    add_type: String,
    rawData: Array,
    selectModel: Object
  },
  created() {

    this.dialogVisible = this.open_add;
    this.accessList = deepClone(this.rawData);
    console.log(this.accessList)
    this.accessList.unshift({ label: '全国', value: 0 });
    if (this.add_type == 'copy') {
      this.formData =formatModel(this.formData,this.selectModel);
      this.findParentsID(this.accessList);
    } else if (this.add_type == 'child') {
      this.formData.parent_id = this.selectModel.access_id;
      this.findParentsID(this.accessList);
    }
  },
  methods: {
    /*选择菜单*/
    handleChange(value) {
      console.log(value)
      this.$set(this.formData,'parentsVal',value)
    },

    //需要使用强制更新
    changeTime(val) {
      this.$forceUpdate()
    },
    /*查找父集id*/
    findParentsID(list){
      let flag=false;
      for(let i=0;i<list.length;i++){
        let item=list[i];
        if(item.access_id==this.formData.parent_id){
          this.parentsVal.unshift(item.access_id);
          flag=true;
          break;
        }else{
          let children=item.children;
          if(typeof children!='undefined'&&children.length>0){
              if(this.findParentsID(children)){
                this.parentsVal.unshift(item.access_id);
                flag=true;
                break;
              }
          }
        }
      }
      return flag;
    },

    /*添加菜单*/
    onSubmit() {
      let self = this;
      let params = this.formData;
      if (self.parentsVal.length > 0) {
        params.areaid = self.parentsVal[self.parentsVal.length - 1];
      }
      self.$refs.form.validate(valid => {
        if (valid) {
          self.loading = true;
          Shoptime.create(params, true)
            .then(res => {
              if (res.code == 1) {
                self.$message({
                  message: res.msg,
                  type: 'success'
                });
                self.dialogFormVisible(true);
                self.loading = false;
              }
            })
            .catch(error => {
              self.loading = false;
            });
        }
      });
    },

    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit('closeDialog', {
          type: 'success',
          openDialog: false
        });
      } else {
        this.$emit('closeDialog', {
          type: 'error',
          openDialog: false
        });
      }
    }
  }
};
</script>

<style></style>
